package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.LiuxuequanMsgResponse;
import com.wephone.Student.bean.LiuxuequanSingleDetailResponse;
import com.wephone.Student.bean.data.LiuxuequanInfoData;
import com.wephone.Student.bean.data.LiuxuequanMsgInfoData;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;

public class LiuXueQuanMsgListActivity extends BaseActivity {
	private ListView listView;
	private ListViewAdapter adapter;
	private View footerView;
	
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanMsgListActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					adapter.getResults().clear();
					adapter.notifyDataSetChanged();
					Toast.makeText(getApplicationContext(), "消息已清空", Toast.LENGTH_SHORT).show();
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler2(){
		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanMsgListActivity.this,LiuxuequanSingleDetailResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					LiuxuequanSingleDetailResponse res = (LiuxuequanSingleDetailResponse)response;
					
					Bundle bundle = new Bundle();
					bundle.putSerializable("infoData", res.getData());
					startActivity(new Intent(getApplicationContext(),LiuXueQuanPinglunDetailActivity.class).putExtras(bundle));
				}
			}
		};
		return handler;
	}

//	private MyResponseHandler getHandler3(){
//		MyResponseHandler handler = new MyResponseHandler(LiuXueQuanMsgListActivity.this,LiuxuequanMsgResponse.class){
//			@Override
//			public void onSuccess(String result) {
//				super.onSuccess(result);
//				
//				if(getDataSuccess){
//					final LiuxuequanMsgResponse res = (LiuxuequanMsgResponse)response;
//					if(res.getData()!=null && res.getData().size()>0){
//						listView.removeFooterView(footerView);
//						adapter.getResults().addAll(res.getData());
//						adapter.notifyDataSetChanged();
//					}
//				}
//			}
//		};
//		return handler;
//	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_liuxuequan_msg_list);
		
		init();
		setResult(22);
	}
	
	private void init(){
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(LiuXueQuanMsgListActivity.this)
				.setTitle("提示")
				.setMessage("您确定要清空所有消息吗?")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						RequestParams params = new RequestParams();
			     		params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						HttpClient.getInstance().get(Constants.API_UPDATE_STUDY_CIRCLE_READ_URL, params, getHandler());
					}
				}).setNegativeButton("取消", null).show();
			}
		});
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			LiuxuequanMsgResponse res = (LiuxuequanMsgResponse)bundle.getSerializable("LiuxuequanMsgResponse");
			if(res!=null && res.getData()!=null){
				listView = (ListView)findViewById(R.id.listview);
				footerView = getLayoutInflater().inflate(R.layout.include_liuxuequan_msg_list_footer, null);
				footerView.findViewById(R.id.btn_see_more).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
//						RequestParams params = new RequestParams();
//				 		params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
//						HttpClient.getInstance().get(Constants.API_GET_STUDY_CIRCLE_READ_URL, params, getHandler3());
						startActivity(new Intent(getApplicationContext(),LiuXueQuanHistoryMsgListActivity.class));
					}
				});
				listView.addFooterView(footerView);
				adapter = new ListViewAdapter(res.getData());
				listView.setAdapter(adapter);
				listView.setOnItemClickListener(new ListView.OnItemClickListener(){
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						// TODO Auto-generated method stub
						LiuxuequanInfoData infoData = adapter.getResults().get(arg2).getStudyDetail();
						
						RequestParams params = new RequestParams();
				 		params.put("id", infoData.getId());
						HttpClient.getInstance().get(Constants.API_GET_STUDY_CIRCLE_DETAIL_URL, params, getHandler2());
					}});
			}
		}
		
	}
	
	private DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.liuxuequan_image_default)
    .showImageOnFail(R.drawable.liuxuequan_image_default)
    .bitmapConfig(Bitmap.Config.ARGB_8888)
    .cacheInMemory()
    .cacheOnDisc()
    .build();
	
	private class ListViewAdapter extends BaseAdapter{
		private List<LiuxuequanMsgInfoData> results;
		private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		public ListViewAdapter(List<LiuxuequanMsgInfoData> results) {
			super();
			this.results = results;
		}

		public List<LiuxuequanMsgInfoData> getResults() {
			return results;
		}

		@Override
		public int getCount() {
			return results.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView = getLayoutInflater().inflate(R.layout.include_liuxuequan_msg_list_item, null);
			}
			LiuxuequanMsgInfoData data = results.get(position);
			TextView itemName = (TextView)convertView.findViewById(R.id.item_name);
			itemName.setText(data.getuInfo().getuName());
			TextView itemContent = (TextView)convertView.findViewById(R.id.item_content);
			if(data.getType()!=null && data.getType().equals("zan")){
				itemContent.setVisibility(View.GONE);
				convertView.findViewById(R.id.item_zan_image).setVisibility(View.VISIBLE);
			}else if(data.getType()!=null && data.getType().equals("comment")){
				itemContent.setVisibility(View.VISIBLE);
				convertView.findViewById(R.id.item_zan_image).setVisibility(View.GONE);
				itemContent.setText(data.getContent());
			}
			TextView itemTime = (TextView)convertView.findViewById(R.id.item_create_time);
			itemTime.setText(sdf.format(new Date(Long.parseLong(data.getCtime())*1000)));
			TextView itemComment = (TextView)convertView.findViewById(R.id.item_comment_text);
			ImageView itemPicImage = (ImageView)convertView.findViewById(R.id.item_liuxuequan_pic_image);
			if(!TextUtils.isEmpty(data.getImg())){
				itemComment.setVisibility(View.GONE);
				itemPicImage.setVisibility(View.VISIBLE);
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getImg(), itemPicImage, options);
			}else{
				itemComment.setVisibility(View.VISIBLE);
				itemPicImage.setVisibility(View.GONE);
				itemComment.setText(data.getScdetail());
			}
			
			RoundImageView itemImage = (RoundImageView)convertView.findViewById(R.id.item_logo_image);
			if(!TextUtils.isEmpty(data.getuInfo().getuIcon())){
				String endStr = data.getuInfo().getuIcon().substring(data.getuInfo().getuIcon().lastIndexOf("."));
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+data.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
			}else{
				itemImage.setImageResource(R.drawable.default_user_logo);
			}
			itemImage.setTag(data.getuInfo());
			itemImage.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					UserInfoData userInfo = (UserInfoData)v.getTag();
					Bundle bundle = new Bundle();
					bundle.putSerializable("userData", userInfo);
					startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
				}
			});
			
			return convertView;
		}		
	}

}
